<template xmlns:el-col="http://www.w3.org/1999/html">
    <div>
<!--        <div class="crumbs">-->
<!--            <el-breadcrumb separator="/">-->
<!--                <el-breadcrumb-item>-->
<!--                    <i class="el-icon-lx-cascades"></i> 版本记录-->
<!--                </el-breadcrumb-item>-->
<!--            </el-breadcrumb>-->
<!--        </div>-->
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.organNm" placeholder="医院名称" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-input v-model="query.machineCode" placeholder="机器型号" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
<!--                <div class="right-items" style="float: right;" >-->
<!--                    <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>-->
<!--                </div>-->
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
            >
                <el-table-column prop="recordId" label="记录编号" width="200" align="center"></el-table-column>
                <el-table-column prop="organNm" label="医院名称" align="center" width="200"></el-table-column>
                <el-table-column v-if="false" prop="machineCode" label="机器型号" align="center" width="100"></el-table-column>
                <el-table-column prop="projectNm" label="项目名称" align="center" width="200"></el-table-column>
                <el-table-column prop="projectEnglishNm" label="英文名称"  align="center" width="150"></el-table-column>
                <el-table-column prop="proPeople" label="下位机对接人"  align="center" width="120"></el-table-column>
                <el-table-column v-if="false" prop="clientVersion" label="客户端版本号"  align="center" width="100"></el-table-column>
                <el-table-column prop="serverVersion" label="服务端版本号"  align="center" width="180"></el-table-column>
                <el-table-column prop="deployDate" label="部署日期"  align="center"></el-table-column>
                <el-table-column prop="istTm" label="插入时间"  align="center"></el-table-column>
<!--                <el-table-column  label="操作" width="120" align="center">-->
<!--                    <template slot-scope="scope">-->
<!--                        <el-button-->
<!--                                type="text"-->
<!--                                icon="el-icon-edit"-->
<!--                                @click="handleEdit(scope.$index, scope.row)"-->
<!--                        >编辑</el-button>-->
<!--                        <el-button-->
<!--                                type="text"-->
<!--                                icon="el-icon-delete"-->
<!--                                class="red"-->
<!--                                @click="handleDelete(scope.$index, scope.row)"-->
<!--                        >删除</el-button>-->
<!--                    </template>-->
<!--                </el-table-column>-->
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="40%" :before-close="editColse">
            <el-form ref="form" :model="form" :rules="addFormRules" label-width="120px" >
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="客户端版本号" prop="clientVersion" >
                            <el-select v-model="form.clientVersion" placeholder="请选择客户端版本号">
                                <el-option v-for="clientVersion in clientVersions" :label="clientVersion" :key="clientVersion" :value="clientVersion"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="服务端版本号" prop="serverVersion">
                            <el-select v-model="form.serverVersion" placeholder="请选择服务端版本号">
                                <el-option v-for="serverVersion in serverVersions" :label="serverVersion" :key="serverVersion" :value="serverVersion"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="机器型号" prop="machineCode">
                            <el-input v-model="form.machineCode" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="部署日期" prop="deployDate" >
                            <el-date-picker style="width: 100%;"
                                            v-model="form.deployDate"
                                            type="date"
                                            placeholder="选择日期"
                                            value-format="yyyy-MM-dd"
                                            :picker-options="projectOptions">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="器械地址" prop="machineAddress">
                            <el-input   type="textarea"  v-model="form.machineAddress" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editColse">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="40%" :before-close="closeAdd">
            <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="120px">
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="医院名称" prop="organId">
                            <el-select v-model="addForm.organId" placeholder="请选择医院">
                                <el-option v-for="organ in organs" :label="organ.organNm" :key="organ.organId" :value="organ.organId"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="项目名称" prop="projectId">
                            <el-select v-model="addForm.projectId" placeholder="请选择项目名称">
                                <el-option v-for="project in projects" :label="project.projectNm" :key="project.projectId" :value="project.projectId"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="客户端版本号" prop="clientVersion">
                            <el-select v-model="addForm.clientVersion" placeholder="请选择客户端版本号">
                                <el-option v-for="clientVersion in clientVersions" :label="clientVersion" :key="clientVersion" :value="clientVersion"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="服务端版本号" prop="serverVersion">
                            <el-select v-model="addForm.serverVersion" placeholder="请选择服务端版本号">
                                <el-option v-for="serverVersion in serverVersions" :label="serverVersion" :key="serverVersion" :value="serverVersion"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="机器型号" prop="machineCode">
                            <el-input v-model="addForm.machineCode" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-form-item label="部署日期" prop="deployDate" >
                            <el-date-picker style="width: 100%;"
                                            v-model="addForm.deployDate"
                                            type="date"
                                            placeholder="选择日期"
                                            value-format="yyyy-MM-dd"
                                            :picker-options="projectOptions">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="器械地址" prop="machineAddress">
                            <el-input   type="textarea"  v-model="addForm.machineAddress" style="width: 100%;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click='closeAdd'>取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {recordList,selectAllHospital,selectAllProject,selectVersionById,doAdd,selectById,doUpdate,doDelete} from '../../../api/record/record';
    export default {
        name: 'project',
        data() {
            return {
                query: {//检索条件
                    organNm: '',
                    machineCode:'',
                    userAcct:localStorage.getItem('ms_userAcct'),
                    pageIndex: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                addVisible: false,
                addForm:{},
                organs:[],
                projects:[],
                clientVersions:[],
                serverVersions:[],
                addFormRules: {
                    organId: [
                        { required: true, message: '请选择医院名称', trigger: 'blur' },
                    ],
                    projectId: [
                        { required: true, message: '请选择项目名称', trigger: 'blur' },
                    ],
                    machineCode: [
                        { required: true, message: '请输入机器型号', trigger: 'blur' },
                    ],
                    version: [
                        { required: true, message: '请输入版本号', trigger: 'blur' },
                    ],
                    deployDate: [
                        { required: true, message: '请输入部署日期', trigger: 'blur' },
                    ],
                },
                projectOptions:{
                    disabledDate(time) {
                        return time.getTime() > Date.now() - 8.64e6;
                    }
                },
            };
        },
        created() {
            this.getData();
        },
        watch:{
            'addForm.projectId': {
                handler(newProjectId, oldProjectId) {
                    //根据项目ID查询所有版本
                    selectVersionById({projectId:newProjectId}).then(res => {
                        if (res.success == true) {
                            for (let i=0;i<res.data.length;i++){
                                if (res.data[i].type=="00")//服务端
                                {
                                    this.serverVersions.push(res.data[i].version);
                                }else{
                                    this.clientVersions.push(res.data[i].version);
                                }
                            }
                        }
                    })
                }
            },
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                recordList(this.query).then(res => {
                    this.tableData = res.data;
                    this.pageTotal = res.total;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.organNm = "";
                this.query.machineCode = "";
                this.getData();
            },
            // 新增操作
            handleAdd() {
                //查询所有医院
                selectAllHospital().then(res => {
                    if (res.success==true){
                        this.organs=res.data;
                        //查询所有项目
                        selectAllProject().then(res => {
                            if (res.success==true){
                                this.projects=res.data;
                                this.addVisible = true;
                            }else{
                                this.$message.error('查询基本信息失败');
                            }
                        });
                    }else{
                        this.$message.error('查询基本信息失败');
                    }
                });
            },
            /*新增*/
            saveAdd(){
                // validate 函数 表单验证
                this.$refs['addForm'].validate((valid) => {
                    if (valid) {
                        doAdd(this.addForm).then(res => {
                            if (res.success==true && res.data == 1) {
                                this.addVisible=false;
                                this.addForm={};
                                this.getData();
                                this.$message.success('保存成功');
                            } else {
                                this.$message.error('保存失败');
                            }
                        });
                    }
                });
            },
            closeAdd(){//关闭新增弹出框
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.addVisible=false;
                        this.addForm={};
                        this.getData();
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消操作'
                        });
                    });
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        //后台进行删除操作
                        doDelete({"recordId":row.recordId}).then(res => {
                            if (res.success==true && res.data==1){
                                this.$message.success('删除成功');
                                this.getData();
                            }else {
                                this.$message.error('删除失败');
                                this.getData();
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消删除'
                        });
                        this.getData();
                    });
            },
            // 编辑操作
            handleEdit(index, row) {
                 selectById({recordId:row.recordId}).then(res =>{
                    if (res.success==true){
                        this.form=res.data;
                        //根据项目ID查询所有版本
                        selectVersionById({projectId:row.projectId}).then(res => {
                            if (res.success == true) {
                                for (let i=0;i<res.data.length;i++){
                                    if (res.data[i].type=="00")//服务端
                                    {
                                        this.serverVersions.push(res.data[i].version);
                                    }else{
                                        this.clientVersions.push(res.data[i].version);
                                    }
                                }
                            }else{
                                this.$message.error('查询版本信息失败');
                            }
                        });
                        this.editVisible = true;
                    }else{
                        this.$message.error('查询项目失败');
                    }
                });
            },
            // 保存编辑
            saveEdit() {
                // validate 函数 表单验证
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        //修改后的数据发送到后台
                        doUpdate(this.form).then(res => {
                            if (res.success==true && res.data == 1) {
                                this.editVisible = false;
                                this.form = {};
                                this.getData();
                                this.$message.success('编辑成功');
                            } else {
                                this.$message.error('编辑失败');
                            }
                        });
                    }
                })

            },
            editColse(){//关闭编辑弹出框
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.editVisible=false;
                        this.form={};
                        this.getData();
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消操作'
                        });
                    });

            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
        }
    };
</script>
<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .schart {
        width: 550px;
        height: 300px;
    }
    .content-title {
        clear: both;
        font-weight: 400;
        line-height: 50px;
        margin: 10px 0;
        font-size: 22px;
        color: #1f2f3d;
    }
</style>
